home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / pvert.arc / WRITLINE.C < prev    next >
C/C++ Source or Header  |  1992-01-15  |  2KB  |  89 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4.  
  5. /* Return a line from a message */
  6.  
  7. char * write_line (char **text,unsigned int linelen,char ctla) {
  8.  
  9.     static char line[133];
  10.     register unsigned int x = 0;
  11.     char *p;
  12.     char *pp;
  13.  
  14.  
  15.     if(!*text) return "";
  16.     if(!**text) return *text;
  17.     p = *text;
  18.     pp = line;
  19.     *pp = 0;
  20.     while(++x < (linelen + 1)) {
  21.  
  22. ReSwitch:
  23.  
  24.         switch(*p) {
  25.             case '\r':      *pp = 0;
  26.                             p++;
  27.                             goto GotCR;
  28.  
  29.             case '\0':      goto StopIt;
  30.  
  31.             case '\n':
  32.             case '\x8d':    p++;
  33.                             goto ReSwitch;
  34.  
  35.             case '\01':     if(!ctla) {
  36.  
  37.                                 int x = 0;
  38.  
  39.                                 if(p == *text || (*(p - 1) == '\r')) {
  40.                                     while(*p != '\r' && *p &&  x < 128) {
  41.                                         x++;
  42.                                         p++;
  43.                                     }
  44.                                     if(*p == '\r') p++;
  45.                                     goto ReSwitch;
  46.                                 }
  47.                             }
  48.  
  49.             default:        *pp = *p;
  50.                             pp++;
  51.                             p++;
  52.                             *pp = 0;
  53.                             break;
  54.         }
  55.     }
  56.  
  57. StopIt:
  58.  
  59.     if(*p == ' ') {
  60.         *pp = 0;
  61.         while(*p == ' ') p++;
  62.     }
  63.     else if(x == (linelen + 1)) {
  64.         if(strchr(line,' ')) {
  65.             while(p > *text && *pp != ' ') {
  66.                 *pp = 0;
  67.                 pp--;
  68.                 p--;
  69.             }
  70.             if(p == *text) {
  71.                 strncpy(line,*text,linelen + 1);
  72.                 line[linelen + 1] = 0;
  73.                 p += (linelen + 1);
  74.             }
  75.             else p++;
  76.         }
  77.     }
  78.  
  79. GotCR:
  80.  
  81.     while(*pp == ' ' && pp > line) {    /* Rstrip returned string */
  82.         *pp = 0;
  83.         --pp;
  84.     }
  85.  
  86.     *text = p;
  87.     return line;
  88. }
  89.